package com.wlyuan.open.domain.source.dispatcher.conversion;

import com.wlyuan.core.exception.AssertUtils;
import com.wlyuan.core.exception.ValueError;
import com.wlyuan.open.dto.OpenConsigneeSyncDTO;
import com.wlyuan.open.dto.OpenReceivingAddressSyncDTO;
import com.wlyuan.open.remote.facade.Address;
import com.wlyuan.open.remote.facade.AddressDTO;
import com.wlyuan.open.remote.facade.constants.Constants;
import com.wlyuan.open.utils.LongUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * @author lijing
 * @description
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class OpenDispatcherConsigneeConversion implements OpenDispatcherConversion<OpenConsigneeSyncDTO> {
    private final BaseOpenDispatcherConversion baseOpenDispatcherConversion;

    @Override
    public void convert(OpenConsigneeSyncDTO source) {
        var tenant = source.getTenantId();
        if (LongUtils.notNullLong(tenant)) {
            var tenantId = baseOpenDispatcherConversion.tenant(tenant);
            AssertUtils.assertNotNull(tenantId, new ValueError("OpenConsigneeSyncDTO租户不存在:" + tenant), source);
            source.setTenantId(tenantId);
        }
        //创建人
        var createBy = source.getCreateBy();
        if (LongUtils.notNullLong(createBy)) {
            Long user = baseOpenDispatcherConversion.user(createBy);
            AssertUtils.assertNotNull(user, new ValueError("OpenConsigneeSyncDTO创建人不存在:" + createBy), source);
            source.setCreateBy(user);
        }
        //修改人
        var updateBy = source.getUpdateBy();
        if (LongUtils.notNullLong(updateBy)) {
            Long user = baseOpenDispatcherConversion.user(updateBy);
            AssertUtils.assertNotNull(user, new ValueError("OpenConsigneeSyncDTO修改人不存在:" + updateBy), source);
            source.setUpdateBy(user);
        }

        convertArea(source);
    }

    private void convertArea(OpenConsigneeSyncDTO source) {
        if (!CollectionUtils.isEmpty(source.getAddressList())) {
            source.getAddressList().forEach(data -> {
                if (StringUtils.isNotBlank(data.getProvince()) || StringUtils.isNotBlank(data.getCity()) || StringUtils.isNotBlank(data.getDistrict()) || StringUtils.isNotBlank(data.getAddress())) {
                    var from = baseOpenDispatcherConversion.area(new AddressDTO(data.getProvinceCode(), data.getProvince(), data.getCityCode(), data.getCity(), data.getDistrictCode(), data.getDistrict(), data.getAddress(), Constants.LEVEL));
                    if (from != null) {
                        area(data, from);
                    } else {
                        data.setProvince("");
                        logger.error("OpenConsigneeSyncDTO 地址不存在:" + data.getProvince() + "," + data.getCity() + "," + data.getDistrict());
                    }
                }
            });
            source.getAddressList().removeIf(x -> StringUtils.isBlank(x.getProvince()));
        }
    }

    private void area(OpenReceivingAddressSyncDTO detail, Address area) {
        detail.setProvinceCode(area.getProvinceCode());
        detail.setProvince(area.getProvinceName());
        detail.setCityCode(area.getCityCode());
        detail.setCity(area.getCityName());
        detail.setDistrictCode(area.getDistrictCode());
        detail.setDistrict(area.getDistrictName());
        if (StringUtils.isBlank(detail.getLat())) {
            detail.setLat(detail.getLat());
        }
        if (StringUtils.isBlank(detail.getLon())) {
            detail.setLon(detail.getLon());
        }
    }
}
